var api = require('../../../../api.js');
var utils = require('../../../../utils.js');
var WxParse = require('../../../../wxParse/wxParse.js');
var app = getApp();

// pages/integral/record/record.js
Page({
    data: {
        img: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2754155622,3305691578&fm=27&gp=0.jpg',

        winWidth: 0,
        winHeight: 0,
        // tab切换  
        currentTab: 0,
        // 详情 单个兑换记录的详情
        record_show:true,
    },
    // 跳转详情
    todetail:function(e){
        var that = this
        console.log(e)
        var id = e.currentTarget.dataset.id
        var u_jf = that.data.u_jf
        wx.navigateTo({
            url: '/page/integral/pages/shopdetail/shopdetail?id=' + id + '&u_jf=' + u_jf,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    // 防止遮罩层穿透
    myCatchTouch: function () {
        console.log('stop user scroll it!');
        return;
    },
    // 兑换详情
    record_detail:function(e){
        var that = this;
        console.log(e)
        that.setData({
            record_show:!that.data.record_show
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var u_jf = options.u_jf;
        that.setData({
            u_jf: u_jf
        })
        
        
        app.request({
            url: api.integralmall.exchange,
            data: '',
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {

                // to_date
                // var data = res.data.list[0].goodsDetail;     
                var data = res.data.list[0].goodsDetail
                for(var i =0;i<data.length;i++){
                    data[i].addtime = to_date(data[i].addtime);
                }
                
                // for(var i =0;i<data.length;i++){
                //     data[i].order.pay_time = to_date(data[i].order.pay_time);
                // }

                var coupon_list = res.data.list[0].userCoupon;
                console.log(coupon_list)

                for (var j = 0; j < coupon_list.length; j++) {
                    coupon_list[j].coupon.addtime = to_date(coupon_list[j].coupon.addtime);
                    coupon_list[j].coupon.end_time = to_date(coupon_list[j].coupon.end_time);
                }

                console.log(res.data.list[0].goodsDetail);
                that.setData({
                    exchange_list: res.data.list[0].goodsDetail,

                    // exchange_year: res.data,
                    // // coupon
                    coupon: res.data.list[0].userCoupon,
                    u_jf: u_jf

                })

                

            },
            fail: function(res) {},
            complete: function(res) {
                app.request({
                    url: api.integralmall.convertible,
                    data: {
                        integral: u_jf,
                    },
                    header: {},
                    method: 'GET',
                    dataType: 'json',
                    responseType: 'text',
                    success: function (res) {
                        console.log(res.data)
                        console.log("========商品兑换========")
                        that.setData({
                            convertible: res.data
                        })
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                })

            },
        })
        //  手机信息 
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }

        });

    },
    /** 
     * 滑动切换tab 
     */
    bindChange: function(e) {

        var that = this;
        that.setData({
            currentTab: e.detail.current
        });

    },
    /** 
     * 点击tab切换 
     */
    swichNav: function(e) {

        var that = this;

        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})


function to_date(phpstr) {
    var  str = parseInt(phpstr) * 1000;//将php时间戳转化为整形并乘以1000
    var newDate = new Date(str);
    var year = newDate.getUTCFullYear();//取年份
    var month = newDate.getUTCMonth() + 1;//取月份
    var nowday = newDate.getUTCDate();//取天数
    var hours = newDate.getHours();//取小时
    var minutes = newDate.getMinutes();//取分钟
    var seconds = newDate.getSeconds();//取秒
    return year + "-" + month + "-" + nowday + " " + hours + ":" + minutes + ":" + seconds;//拼接 2017-2-21 12:23:43
}